package com.hoshiicloud.user.controller.platform;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.platform.Platform;
import com.hoshiicloud.user.param.platform.PlatFormInsertParam;
import com.hoshiicloud.user.param.platform.PlatFormParam;
import com.hoshiicloud.user.param.platform.PlatFormUpdateParam;
import com.hoshiicloud.user.service.platform.PlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author yl
 * @title: PlaformController
 * @projectName hoshiicloud-platform
 * @description: 平台控制器
 * @date 2019/4/18 10:43
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/platform/platform")
@Api(value = "SAAS租户平台", tags = {"SAAS租户平台"})
@ApiSort(21)
public class PlaformController {
    @Autowired
    private PlatformService platformService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询平台信息
     */
    @ApiOperation(value = "分页查询平台记录")
    @PostMapping("/getPage")
    public Response page(@RequestBody PlatFormParam param) {
        try {
            QueryWrapper<Platform> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(StringUtils.isNotBlank(param.getPlatformName()), Platform::getPlatformName, param.getPlatformName());
            queryWrapper.lambda().eq((null != param.getCompanyId() && param.getCompanyId() != 0), Platform::getCompanyId, param.getCompanyId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(param.getLegalPersionName()), Platform::getLegalPersionName, param.getLegalPersionName());
            IPage<Platform> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(platformService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询平台信息异常", e);
            return Response.failed("分页查询平台信息异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询平台信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询平台信息")
    public Response queryPlatformById(@PathVariable("id") Long id) {
        return Response.data(platformService.getById(id));
    }

    /**
     * @param platformName
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据名称查询平台信息
     */
    @GetMapping("/query/platformName/{platformName}")
    @ApiOperation(value = "查询平台信息")
    public Response queryPlatformByName(@PathVariable("platformName") String platformName) {
        return Response.data(platformService.platformName(platformName));
    }

    /**
     * @param platFormInsertParam
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台信息")
    public Response savePlatform(@RequestBody @Valid PlatFormInsertParam platFormInsertParam) {
        Platform entity;
        try {
            entity = BeanUtils.covertType(platFormInsertParam, Platform.class);
        } catch (Exception e) {
            log.error("新增平台信息Bean转换异常", e);
            return Response.failed("新增平台信息Bean转换异常");
        }
        return Response.data(platformService.save(entity));
    }

    /**
     * @param platFormUpdateParam
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台信息")
    public Response updatePlatformById(@RequestBody @Valid PlatFormUpdateParam platFormUpdateParam) {
        Platform entity;
        try {
            entity = BeanUtils.covertType(platFormUpdateParam, Platform.class);
        } catch (Exception e) {
            log.error("修改平台信息Bean转换异常", e);
            return Response.failed("修改平台信息Bean转换异常");
        }
        return Response.data(platformService.updateById(entity));
    }
}
